﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;

namespace Hotel.Models.DataAccess
{
    public class DBManager : IDisposable
    {
        public static readonly string ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        private MySqlCommand command;
        private MySqlConnection connection;

        private void Dispose(bool dis)
        {
            if (dis)
            {
                connection.Open();
            }
            else
            {
                connection.Close();
            }
        }

        public DBManager(string commandText)
        {
            connection = new MySqlConnection(ConnectionString);
            Dispose(true);
            command = connection.CreateCommand();
            command.CommandText = commandText;
            command.CommandType = CommandType.StoredProcedure;
        }

        public MySqlParameter Add(string param, object value)
        {
            return command.Parameters.AddWithValue(param, value);
        }

        public DataTable GetDatatable()
        {
            MySqlDataAdapter da = new MySqlDataAdapter(command);
            DataTable dtable = new DataTable();
            da.Fill(dtable);
            Dispose(false);
            return dtable;
        }

        public DataSet GetDataset()
        {
            MySqlDataAdapter da = new MySqlDataAdapter(command);
            DataSet dset = new DataSet();
            da.Fill(dset);
            Dispose(false);
            return dset;
        }

        public void Dispose()
        {

        }

        public int ExecuteNonQuery()
        {
            return command.ExecuteNonQuery();
        }
    }
}